package org.osami.commons.deviceidentifier.android.impl;

import org.osami.commons.deviceidentifier.IDeviceIdentifier;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import android.content.Context;
import android.telephony.TelephonyManager;

public class DeviceIdentifier implements IDeviceIdentifier {	
	private BundleContext aBc;
	Context context;
	
	public DeviceIdentifier(BundleContext bc) {
		aBc = bc;
		System.out.println("DeviceIdentifier created");
	}

	public String GetDeviceId() {
		String id ="unknown";
		//String javaVendor = System.getProperty("java.vendor").toLowerCase();		
		//if(javaVendor.indexOf("android")>-1) {
		ServiceReference serviceRef = aBc.getServiceReference(Context.class.getName());

		if (serviceRef != null) {
			context = (Context) aBc.getService(serviceRef);			
			TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
			id = tm.getDeviceId();//the IMEI code
		} else {
			System.out.println("Android context not found");
		}
//		}
		return id;
	}
}
